/*
 *  Prefs.m: Preferences of Gomoku.app
 *
 *  Copyright (c) 2004 Adam Nohejl <adam.nohejl@kppm.cz>
 *
 *  Author: Adam Nohejl
 *  Date: 2004-03-13 - 2004-04-08
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#import "Prefs.h"
#import "Board.h"		/* computer, human, thm* */
#import "Controller.h"

#define boardSizeKey		@"BoardSize"
#define diffLevelKey		@"DifficultyLevel"
#define firstTurnKey		@"FirstTurn"
// Added by Li Wei Nan
#define gameTypeKey			@"GameType"

#define boardZoomKey		@"BoardZoom"
#define boardThemeKey		@"BoardTheme"
#define soundsKey			@"SoundsEnabled"

#define boardZoomDefault	@"40"


@implementation Prefs
- (id)init{
	NSDictionary*	appDefaults;
	NSUserDefaults*	defaults;

	
	defaults	= [NSUserDefaults standardUserDefaults];
    appDefaults	= [NSDictionary dictionaryWithObjectsAndKeys:
		@"13",											boardSizeKey,
		@"3",											diffLevelKey,
		[NSString stringWithFormat:@"%i",human],		firstTurnKey,
		[NSString stringWithFormat:@"%i",h2h],		gameTypeKey,
		boardZoomDefault,								boardZoomKey,
		[NSString stringWithFormat:@"%i",thmClassic],	boardThemeKey,
		@"YES",											soundsKey,
		nil
		];
	[defaults registerDefaults:appDefaults];

	boardSize			= [defaults integerForKey:	boardSizeKey];
	if(boardSize<8)
		boardSize = 8;
	else if(boardSize>32)
		boardSize = 32;

	diffLevel			= [defaults integerForKey: 	diffLevelKey];
	if(diffLevel<0)
		diffLevel = 0;
	else if(diffLevel>6)
		diffLevel = 6;	

	firstTurn			= [defaults integerForKey:	firstTurnKey];
	if(firstTurn!=computer)
		firstTurn = human;

	boardZoom			= [defaults integerForKey:	boardZoomKey];
	if(boardZoom%5)
		boardZoom = boardZoom/5*5;
	if(boardZoom<20)
		boardZoom = 20;
	else if(boardZoom>40)
		boardZoom = 40;

	boardTheme			= [defaults integerForKey:	boardThemeKey];
	if(boardTheme!=thmClassic)
		boardTheme = thmOX;
	
	sounds				= [defaults boolForKey: 	soundsKey];
	
	gameType = [defaults integerForKey: gameTypeKey];

	return self;
}

- (int)	boardSize{
	return boardSize;
}
- (int)	diffLevel{
	return diffLevel;
}
- (int)	firstTurn{
	return firstTurn;
}
- (int) gameType{
	return gameType;
}
- (int)	boardZoom{
	return boardZoom;
}
- (int)	boardTheme{
	return boardTheme;
}
- (BOOL)sounds{
	return sounds;
}

- (IBAction)setFirstTurn:(id)sender{
	firstTurn = [[sender selectedItem] tag];
	[[NSUserDefaults standardUserDefaults] setInteger: firstTurn forKey:firstTurnKey];
	if(![controller newGameIfUserHadNotPlayed])
		[self changeFieldBlink];
}

/* added by Li Wei Nan */
- (IBAction)setGameType:(id)sender{
	gameType = [[sender selectedItem] tag];
	//NSLog([NSString stringWithFormat:@"game type is: %d now", gameType]);
	[[NSUserDefaults standardUserDefaults] setInteger: gameType forKey:gameTypeKey];
}

- (IBAction)setLevel:(id)sender{
	diffLevel = [[sender selectedItem] tag];
	[[NSUserDefaults standardUserDefaults] setInteger: diffLevel forKey:diffLevelKey];
	[controller setDifficultyLevel: diffLevel];
}

- (IBAction)setSize:(id)sender{
	boardSize = [sender intValue];
	[[NSUserDefaults standardUserDefaults] setInteger: boardSize forKey:boardSizeKey];
	if(![controller newGameIfUserHadNotPlayed])
		[self changeFieldBlink];
}

- (IBAction)setSounds:(id)sender{
	sounds = [[sender selectedItem] tag];
	[[NSUserDefaults standardUserDefaults] setBool: sounds forKey:soundsKey];
	[controller setSounds:sounds theme:boardTheme zoom:boardZoom];
}

/* TODO */
- (IBAction)setTheme:(id)sender{
	boardTheme = [[sender selectedItem] tag];
	[[NSUserDefaults standardUserDefaults] setInteger: boardTheme forKey:boardThemeKey];
	[controller setSounds:sounds theme:boardTheme zoom:boardZoom];
}

/* TODO */
- (IBAction)setZoom:(id)sender{
	boardZoom = [sender intValue];
	[[NSUserDefaults standardUserDefaults] setInteger: boardZoom forKey:boardZoomKey];
	[controller setSounds:sounds theme:boardTheme zoom:boardZoom];
}

- (IBAction)showWin:(id)sender{
	if(![prefsWin isVisible]){
		/*[prefsWin setFrameAutosaveName:prefsWinFAName]; TODO */
		[firstTurnPopUp	selectItemAtIndex: [firstTurnPopUp indexOfItemWithTag:firstTurn]];
		[levelPopUp		selectItemAtIndex: [levelPopUp indexOfItemWithTag:diffLevel]];
		[sizeSlider		setIntValue:boardSize];
		[soundsPopUp	selectItemAtIndex: [soundsPopUp indexOfItemWithTag:sounds]];
		[themePopUp		selectItemAtIndex: [themePopUp indexOfItemWithTag:boardTheme]];
		[zoomSlider		setIntValue:boardZoom];
		[changeField	setTextColor: [NSColor colorWithCalibratedWhite:0.0 alpha:0.0]];
	}
	[prefsWin makeKeyAndOrderFront:nil];
}

/* set up controller's board */
- (void)setUp{
	[controller setSounds:sounds theme:boardTheme zoom:boardZoom];
	[controller setDifficultyLevel: diffLevel];
}

/*- (BOOL)winIsKey{
	return [prefsWin isKeyWindow];
}*/

/* blinking the "Changes will take effect only in the next game." warning */

- (void)changeFieldBlink{
	changeFieldBlinks++;
	if(!changeFieldBlinkTimer)
		changeFieldBlinkTimer = [NSTimer scheduledTimerWithTimeInterval:0.06 target:self selector:@selector(changeFieldBlinkUpdate:) userInfo:nil repeats:YES];
}

- (void)changeFieldBlinkUpdate:(NSTimer*)timer{
	if(!changeFieldBlinks){
		[changeFieldBlinkTimer invalidate];
		changeFieldBlinkTimer = nil;
		return;
	}
	if(changeFieldBlinkPhase>=30){
		changeFieldBlinkPhase = 0;
		changeFieldBlinks--;
	}

	if(changeFieldBlinkPhase<5)
		[changeField setTextColor: [NSColor colorWithCalibratedWhite:0.0 alpha:0.1*changeFieldBlinkPhase]];
	else if(changeFieldBlinkPhase<25)
		[changeField setTextColor: [NSColor colorWithCalibratedWhite:0.0 alpha:1]];
	else
		[changeField setTextColor: [NSColor colorWithCalibratedWhite:0.0 alpha:0.2*(29-changeFieldBlinkPhase)]];

	changeFieldBlinkPhase++;
}

@end
